﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace PresentationWpf.XMlWorker
{
    public class XMLExporter : XMLWorker
    {
         public static void export(ref Arbore.Arbore arb){
          SaveFileDialog dlg = new SaveFileDialog();
			dlg.FileName =  arb.Radacina.Value+ ".xml";
			dlg.Filter = "XML Files (*.xml)|*.xml";			
			if (dlg.ShowDialog() == DialogResult.OK)
			{
				exportToXml2(ref arb, dlg.FileName);
			}
		}	

		//We use this in the exportToXml2 and the saveNode2 functions, though it's only instantiated once.
		private static XmlTextWriter xr;

        private static void exportToXml2(ref Arbore.Arbore arb, string filename) 
		{
			xr = new XmlTextWriter(filename, System.Text.Encoding.UTF8);

			xr.WriteStartDocument();
			//Write our root node
            //string st=
            xr.WriteStartElement("tt");
			foreach (Arbore.Nod node in arb.Radacina.Childrens)
			{
                if (node != null)
                {
                    saveNode2(node.Childrens);
                }
			}
			//Close the root node
			xr.WriteEndElement();
			xr.Close();
		}

        private static void saveNode2(List<Arbore.Nod> Childrens)
		{
            foreach (Arbore.Nod node in Childrens)
			{
				//If we have child nodes, we'll write a parent node, then iterrate through
				//the children
                if (node != null)
                {
                    if (node.Childrens.Count > 0)
                    {
                        xr.WriteStartElement("hg");
                        saveNode2(node.Childrens);
                        xr.WriteEndElement();
                    }
                    else //No child nodes, so we just write the text
                    {

                        xr.WriteStartElement("hg");
                         xr.WriteEndElement();
                    }
                }
			}
		}
       
    }
}
